package com.CardBudgetBook.Main;

import java.util.*;

import android.content.*;
import android.graphics.*;
import android.view.*;
import android.widget.*;

import com.CardBudgetBook.*;

/**
 * BaseAdapter占쏙옙 占쏙옙達騁占�占쏙옙占쏙옙占쏙옙 CalendarAdapter
 * 
 * @author croute
 * @since 2011.03.08
 */
public class CalendarAdapter extends BaseAdapter {
	private ArrayList<DayInfo> mDayList;
	private Context mContext;
	private int mResource;
	private LayoutInflater mLiInflater;

	/**
	 * Adpater 占쏙옙占쏙옙
	 * 
	 * @param context
	 *            占쏙옙占쌔쏙옙트
	 * @param textResource
	 *            占쏙옙占싱아울옙 占쏙옙占쌀쏙옙
	 * @param dayList
	 *            占쏙옙짜占쏙옙占쏙옙占쏙옙 占쏙옙占쏙옙獵占�占쏙옙占쏙옙트
	 */
	public CalendarAdapter(Context context, int textResource,
			ArrayList<DayInfo> dayList) {
		this.mContext = context;
		this.mDayList = dayList;
		this.mResource = textResource;
		this.mLiInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getCount()
	 */
	public int getCount() {
		// TODO Auto-generated method stub
		return mDayList.size();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getItem(int)
	 */
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mDayList.get(position);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getItemId(int)
	 */
	public long getItemId(int position) {
		return 0;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getView(int, android.view.View,
	 * android.view.ViewGroup)
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		DayInfo day = mDayList.get(position);

		DayViewHolde dayViewHolder;

		if (convertView == null) {
			convertView = mLiInflater.inflate(mResource, null);

			if (position % 7 == 6) {
				convertView.setLayoutParams(new GridView.LayoutParams(
						getCellWidthDP() + getRestCellWidthDP(),
						getCellHeightDP()));
			} else {
				convertView.setLayoutParams(new GridView.LayoutParams(
						getCellWidthDP(), getCellHeightDP()));
			}

			dayViewHolder = new DayViewHolde();

			dayViewHolder.llBackground = (LinearLayout) convertView
					.findViewById(R.id.day_cell_ll_background);
			dayViewHolder.tvDay = (TextView) convertView
					.findViewById(R.id.Main_TextView_DayCell);
			dayViewHolder.pbOutlay = (ProgressBar)  convertView
					.findViewById(R.id.day_cell_pb_outlay);

			convertView.setTag(dayViewHolder);
		} else {
			dayViewHolder = (DayViewHolde) convertView.getTag();
		}

		
		if (day != null) {
			dayViewHolder.tvDay.setText(day.getDay());
			if (day.isInMonth()) {
				if (position % 7 == 0) {
					dayViewHolder.tvDay.setTextColor(Color.RED);
				} else if (position % 7 == 6) {
					dayViewHolder.tvDay.setTextColor(Color.BLUE);
				} else {
					dayViewHolder.tvDay.setTextColor(Color.BLACK);
				}
				dayViewHolder.pbOutlay.setProgress(Integer.parseInt(day.getDay()));
			} else {
				dayViewHolder.tvDay.setTextColor(Color.GRAY);
				dayViewHolder.pbOutlay.setVisibility(View.INVISIBLE);
			}
			if(Calendar.DATE == 2)
			{
				dayViewHolder.tvDay.setTextColor(Color.GREEN);
			}
		}

		return convertView;
	}

//	public class CustomView extends SurfaceView implements
//			SurfaceHolder.Callback {
//		private CustomViewThread CVThread;
//
//		public CustomView(Context context) {
//			super(context);
//			getHolder().addCallback(this);
//			CVThread = new CustomViewThread(getHolder(), this);
//			setFocusable(true);
//		}
//
//		@Override
//		public void onDraw(Canvas canvas) {
//
//			canvas.drawColor(Color.parseColor("#ff456789"));
//			   //�쇱� 洹몃━湲�
//			
//			    Paint p = new Paint();
//			           p.setAntiAlias(true);
//			           p.setStrokeWidth(5);
//			           p.setColor(Color.RED);
//			
//			            canvas.drawLine(0, 0, 100, 100, p);
//			// 洹몃┝��洹몃┛��
//
//		}
//
//		public void surfaceChanged(SurfaceHolder holder, int format, int width,
//				int height) {
//			// CVThread.setRunning(true);
//			// CVThread.start();
//			// TODO Auto-generated method stub
//
//		}
//
//		public void surfaceCreated(SurfaceHolder holder) {
//			CVThread.setRunning(true);
//			CVThread.start();
//		}
//
//		public void surfaceDestroyed(SurfaceHolder holder) {
//
//			boolean retry = true;
//			CVThread.setRunning(false);
//			while (retry) {
//				try {
//					CVThread.join();
//					retry = false;
//				} catch (InterruptedException e) {
//
//				}
//			}
//		}
//	}
//
//	public class CustomViewThread extends Thread {
//		private SurfaceHolder surfaceholder;
//		private CustomView customview;
//		private boolean running = false;
//
//		public CustomViewThread(SurfaceHolder surfaceHolder,
//				CustomView CustomView) {
//			surfaceholder = surfaceHolder;
//			customview = CustomView;
//		}
//
//		public void setRunning(boolean run) {
//			running = run;
//		}
//
//		public SurfaceHolder getSurfaceHolder() {
//			return surfaceholder;
//		}
//
//		@Override
//		public void run() {
//			Canvas c;
//			while (running) {
//				c = null;
//				try {
//					c = surfaceholder.lockCanvas(null);
//					synchronized (surfaceholder) {
//						customview.onDraw(c);
//					}
//				} finally {
//
//					if (c != null) {
//						surfaceholder.unlockCanvasAndPost(c);
//					}
//				}
//			}
//		}
//	}

	public class DayViewHolde {
		public LinearLayout llBackground;
		public TextView tvDay;
    	public ProgressBar pbOutlay;

	}

	private int getCellWidthDP() {
		// int width = mContext.getResources().getDisplayMetrics().widthPixels;
		int cellWidth = 480 / 7;

		return cellWidth;
	}

	private int getRestCellWidthDP() {
		// int width = mContext.getResources().getDisplayMetrics().widthPixels;
		int cellWidth = 480 % 7;

		return cellWidth;
	}

	private int getCellHeightDP() {
		// int height = mContext.getResources().getDisplayMetrics().widthPixels;
		int cellHeight = 480 / 6;

		return cellHeight;
	}

}
